<template>
	<view>
		<view class="banner" v-show="goodsMsg.primaryPicUrl != ''">
			<image class="banner-img" :src="goodsMsg.primaryPicUrl"></image>
		</view>
		<view class="f-header m-t">
			<view class="tit-box">
				{{goodsMsg.goodsSn}}&nbsp;&nbsp;&nbsp;&nbsp;<text class="tit">{{goodsMsg.name}}</text>
			</view>
		</view>
		<view class="article-meta">
			<view class="uni-media-list-logo">
				<image src="/static/logo.png"></image>
			</view>
			<text class="article-author">{{goodsMsg.brandName?goodsMsg.brandName:''}}</text>
			<text class="article-time">{{publicDate}}</text>
			<!-- #ifndef MP -->
			<text class="article-text price" v-if="goodsMsg.unitPrice>0">￥{{goodsMsg.unitPrice}}</text>
			<!-- #endif -->
		</view>
		<view class="uni-card uni-list" v-if="isAudio">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" >
				<view class="uni-media-list">
					<view v-if="playing" class="uni-media-list-logo bg_img" @tap="stop" >
						<image  src="../../static/stop.png"></image>
					</view>
					<view v-if="!playing" class="uni-media-list-logo bg_img" @tap="play">
						<image  src="../../static/play.png"></image>
					</view>
					<view class="uni-media-list-body">
						<view class="uni-media-list-text-bottom uni-ellipsis ">
							<slider block-color="#8A6DE9" class="slider" min="0" :max="musicDuration" step="1" block-size="10" :value="playTime" @change="seek"></slider>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="uni-padding-wrap" v-if="isAudio">
			<view class="uni-center">
				<text class="time-big">{{formatedPlayTime}}{{formatedAllTime}}</text>
			</view>
		</view>
		<view class="article-content" >
			<rich-text :style="[{fontSize: fontsize + 'rpx'}]" :nodes="htmlNodes"></rich-text>
		</view>
		<!-- 底部操作菜单 -->
		<view class="page-bottom">
			<navigator url="/pages/index/index" open-type="switchTab" class="p-b-btn">
				<text class="yticon icon-xiatubiao--copy"></text>
				<text>首页</text>
			</navigator>
			<!-- #ifndef MP -->
			<view class="p-b-btn" @click="share">
				<text class="yticon icon-share"></text>
				<text>分享</text>
			</view>
			<!-- #endif -->
			<view class="p-b-btn"  @click="changeFontSize(5)">
				<uni-icon type="plus" size="26" />
				<text>放大</text>
			</view>
			<view class="p-b-btn"  @click="changeFontSize(-5)">
				<uni-icon type="minus" size="26" />
				<text>缩小</text>
			</view>
			<view class="action-btn-group">
				<button v-if="hasPermission" type="primary" class=" action-btn no-border buy-now-btn" @click="lastArticle">上一篇</button>
				<button v-if="hasPermission" type="primary" class=" action-btn no-border add-cart-btn" @click="nextArticle">下一篇</button>
				
				<button v-if="!hasLogin && !hasPermission" type="primary" class=" action-btn no-border buy-now-btn" @click="login">立即登录</button>
				<button v-if="!hasLogin && !hasPermission" type="primary" class=" action-btn no-border add-cart-btn" @click="share">点击分享</button>
				
				<button v-if="hasLogin && !hasPermission" type="primary" class=" action-btn no-border buy-now-btn" @click="buy">单篇购买</button>
				<button v-if="hasLogin && !hasPermission" type="primary" class=" action-btn no-border add-cart-btn" @click="buyAll">成为会员</button>
			</view>
		</view>
		<!-- #ifndef MP -->
		<view class="mask" 
			:class="showShareClass" 
			@click="hideShareView">
			<image src="../../static/live_weixin.png"  />
		</view>
		<!-- #endif -->
		<!-- #ifdef MP -->
		<uni-popup :show="type === 'middle-img'" position="middle" mode="fixed" @hidePopup="togglePopup('')">
			<view class="uni-center center-box">
				<image class="image" src="/static/kefu.jpg" />
			</view>
		</uni-popup>
		<!-- #endif -->
	</view>
</template>

<script>
	var util = require('../../common/util.js');
	import htmlParser from '@/common/html-parser'
    import commonOnLoad from '../../common/common_on_load.js';  
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import uniIcon from '@/components/uni-icon/uni-icon.vue';

	import {
		mapState,
		mapGetters
	} from 'vuex'
	var playTimeInterval = null;
	var preludeMusicInterval = null;
	
	export default {
		components: {
			uniPopup,
			uniIcon,
		},
		computed: {
			...mapGetters(['hasLogin']),
		},
		data() {
			return {
				isAutoPlay:false,
				isCanPlay:false,
				type:'',
				isAudio:false,
				showShareClass:'none',
				hasPermission:false,
				id:null,
				show_banner_title:true,
				title: 'backgroundAudio',
				favorite: false,
				innerAudioContext: null,
				dataUrl: '',
				playing: false,
				playTime: 0,
				formatedPlayTime: '00:00:00',
				formatedAllTime:'',
				htmlNodes: [],
				musicDuration:100,
				shareList: [],
				goodsMsg:{
					primaryPicUrl:'',
					name:'',
					brandName:'',
					updateTime:'',
				},
				from:null,
				publicDate:'',
				fontsize:30,
			};
		},
		onLoad(options) {
			commonOnLoad.commonOnLoad(this.$mRoutesConfig.article_detail,options,options.referenceId);//options.referenceId
			if(!this.$store.state.preludeMusic){
				let preludeMusic = uni.createInnerAudioContext();
				preludeMusic.autoplay = false;
				this.$store.commit("SET_PRELUDE_MUSIC", preludeMusic);
			}
			this.$store.commit("SET_IS_PRELUDE_MUSIC_FINISHED", false);
			if (options && options.from && !this.$store.hasShareReaded) {
				this.from = options.from;
			}
			if (options && options.id) {
				this.id = options.id;
				if(!this.$store.state.goodsMsgCache || this.$store.state.goodsMsgCache.id != this.id ){
					this.getArticle(this.id);
				}else{
					this.goodsMsg = this.$store.state.goodsMsgCache;
					this.showArticle();
				}
			}
			// #ifdef MP
			if (!this.$store.getters.hasLogin ) {
				this.$store.commit("SET_IS_SILENT_LOGIN", true);
				this.silentLogin();
			}
			// #endif
		},
		onShareAppMessage() {
			let path = "/pages/detail/article_detail?id="+ this.id;
			if(this.$store.getters.hasLogin){
				path = path + "&referenceId=" + this.$store.state.userInfo.id;
			}
			return {
				title: this.goodsMsg.name ? this.goodsMsg.name : this.$mConfig.descTxt,
				path: path,
				imageUrl:this.goodsMsg.listPicUrl ? this.goodsMsg.listPicUrl : this.$mConfig.logoImgUrl
			}
		},
		methods: {
			showKeFu(){
				//this.showMaskClass = 'show';
				 // 预览图片
				uni.previewImage({
					urls: [this.$mConfig.keFuImgUrl],
				});
			},
			changeFontSize(size){
				if(this.fontsize<21)
					return;
				this.fontsize = this.fontsize + size;
			},
			async getArticle(id) {
				var query = {
					from:this.from,
					referenceId:this.$store.state.referenceId
				};
				let data = await this.$apis.getArticleById(id,query);
				this.goodsMsg = data.goods;
				this.$store.commit("SET_GOODS_MSG_CACHE", this.goodsMsg);
				if(this.goodsMsg.buyed){
					this.$store.commit("SET_HAS_SHARE_READED", true);
				}
				this.showArticle();
			},
			showArticle(){
		　　　　 uni.pageScrollTo({ 　　　　　　
		　　　　		duration: 0,//过渡时间必须为0，否则运行到手机会报错
		　　　　		scrollTop: 0 //滚动到实际距离是元素距离顶部的距离减去最外层盒子的滚动距离（如res.top - data.top）
		　　　　 });
				this.title = this.goodsMsg.name;
				this.publicDate = this.goodsMsg.updateTime.substr(0,10);
				//uni.setNavigationBarTitle({
				//	title: this.title,
				//});
				var htmlString = null;
				
				if(this.goodsMsg.buyed){
					htmlString = this.goodsMsg.goodsDesc.replace(/\\/g, "").replace(/<img/g, "<img style=\"display:none;\"");
					this.hasPermission = true;
				}else{
					htmlString = this.goodsMsg.testContent.replace(/\\/g, "").replace(/<img/g, "<img style=\"display:none;\"");
				}
				if(this.goodsMsg.collect > 0){
					this.favorite = true;
				}
				this.htmlNodes = htmlParser(htmlString);
				//share
				setTimeout(() => {
					this.showImg = true;
					// #ifdef H5  
					if (this.$wechat && this.$wechat.isWechat()) {  
						var url = this.$mConfig.baseUrl + "/api/shorturl/" + this.$mConfig.mpAppId + "/articledetail?id=" + this.id;
						if(this.$store.getters.hasLogin){
							url = url + "&referenceId=" + this.$store.state.userInfo.id;
						}
						 this.$wechat.share({  
							  desc: this.goodsMsg.goodsBrief,  
							  img: this.goodsMsg.listPicUrl,  
							  title:this.goodsMsg.name,  
						},url);  
					}  
					// #endif  
				}, 1000);
			},
			async lastArticle(){
				var query = {
					from:this.from,
					referenceId:this.$store.state.referenceId
				};
				let data = await this.$apis.lastArticle(this.id,query);
				if(!data.goods){
					this.$mUtils.msg('这是第一篇');
					return;
				}
				this.goodsMsg = data.goods;
				this.id = data.goods.id;
				this.$store.commit("SET_GOODS_MSG_CACHE", this.goodsMsg);
				if(this.goodsMsg.buyed){
					this.$store.commit("SET_HAS_SHARE_READED", true);
				}
				this.showArticle();
			},
			async nextArticle(){
				var query = {
					from:this.from,
					referenceId:this.$store.state.referenceId
				};
				let data = await this.$apis.nextArticle(this.id,query);
				console.log(JSON.stringify(data));
				if(!data.goods){
					this.$mUtils.msg('这是最后一篇');
					return;
				}
				this.goodsMsg = data.goods;
				this.id = data.goods.id;
				this.$store.commit("SET_GOODS_MSG_CACHE", this.goodsMsg);
				if(this.goodsMsg.buyed){
					this.$store.commit("SET_HAS_SHARE_READED", true);
				}
				this.showArticle();
			},
			//收藏
			async toggleCollect(){
				var query = {};
				if(this.favorite){  //取消
					let data = await this.$apis.delCollect(this.id,query);
					this.goodsMsg.collect = 0;
				}else{
					let data = await this.$apis.addCollect(this.id,query);
					this.goodsMsg.collect = 1;
				}
				this.favorite = !this.favorite;
			},
			async buy(){
				// #ifdef H5  
				var query = {};
				let data = await this.$apis.wxPayByGoodsId(this.goodsMsg.id,1,query);
				if (this.$wechat && this.$wechat.isWechat()) {  
					 this.$wechat.callPay(data,function(){
						uni.showToast({
							title: "支付成功!",
							icon:'none',
							mask:false,
							duration: 2000
						});
						 //后台需确认订单情况 延迟执行
						 setTimeout(() => {
							 this.getGoods(this.id);
						 }, 2000);
					 });  
				}  
				// #endif
				// #ifdef MP-WEIXIN
				uni.previewImage({
					urls: [this.$mConfig.keFuImgUrl],
				});
				// #endif
			},
			buyAll(){
				this.$mRouter.push({
					route: this.$mRoutesConfig.toVip,
					query: {
						id: this.goodsMsg.parentId
					}
				})
			},
			learnMore(){
				this.$mRouter.push({
					route: this.$mRoutesConfig.category,
					query: {
						id: this.goodsMsg.categoryId
					}
				})
			},
			share(){
				// #ifdef H5  
				this.showShareClass = 'show';
				// #endif  
				// #ifdef MP-WEIXIN
				let shareOPtions = {
					provider: "weixin",
					scene: 'WXSceneSession',   
					type: 5,
					success: (e) => {
						console.log('success', e);
					},
					fail: (e) => {
						console.log('fail', e)
					},
					complete:function(){
						console.log('分享操作结束!')
					}
				};
				shareOPtions.imageUrl = this.goodsMsg.listPicUrl;
				shareOPtions.title = this.goodsMsg.name;
				let path = "/pages/detail/article_detail?id="+ this.id;
				if(this.$store.getters.hasLogin){
					path = path + "&referenceId=" + this.$store.state.userInfo.id;
				}
				
				shareOPtions.miniProgram = {
					id:this.$mConfig.xcxId,
					path: path,
					webUrl:this.$mConfig.baseUrl + "/api/shorturl/" + this.$mConfig.mpAppId + "/articledetail?id=" + this.id,
					type: 0
				};
				console.log(JSON.stringify(shareOPtions));
				uni.share(shareOPtions);
				// #endif  
			},
			hideShareView(){
				this.showShareClass = 'hide';
				setTimeout(() => {
					this.showShareClass = 'none';
				}, 250);
			},
			login(){
				this.$store.dispatch("reLogin");
			},
			stopPrevent(){
			},
			togglePopup(type) {
				this.type = type;
			}
		}
	}
</script>

<style lang='scss'>
	.mask {
		position:fixed;
		top:0; left:0;
		right:0;
		bottom:0;
		background:rgba(0,0,0,0.6);
		z-index:9999;
		image{
			width:100%;
		}
		&.show {
			display: block;
			.mask{
				animation: showPopup 0.2s linear both;
			}
			.layer {
				animation: showLayer 0.2s linear both;
			}
		}
		&.hide {
			.mask{
				animation: hidePopup 0.2s linear both;
			}
			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}
		&.none {
			display: none;
		}
	}

	.banner {
		height: 360upx;
		overflow: hidden;
		position: relative;
		background-color: #ccc;
	}
	
	.banner-img {
		width: 100%;
		max-height:100%
	}
	.kefu_img{
		display:table-cell;
		text-align:center;
		vertical-align:middle;
		image{
			margin:0upx 50upx;
			max-width:100%;
			max-height:100%;
		}
	}
	.uni-media-list{
		padding: 5upx ;
	}
	.uni-media-list-body{
		height: 84upx;
		display: flex;
		flex: 1;
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-start;
		overflow: hidden;
	}
	.bg_img {
		// #ifdef H5  
		//background: url(../../static/logo.png); 
		// #endif
		background-size:cover;
		text-align:center;
		display: flex; 
		justify-content: center;
		align-items: center;
		height: 84upx;
		width: 84upx;
	}
  
	.bg_img image{
		width: 100%;
		height:100%;
		opacity: 0.8;
	}
  
	.banner-title {
		max-height: 84upx;
		overflow: hidden;
		position: absolute;
		left: 30upx;
		bottom: 30upx;
		width: 90%;
		font-size: 32upx;
		font-weight: 400;
		line-height: 42upx;
		color: white;
		z-index: 11;
	}
	.page-body-text {
		padding: 0 30upx;
	}
	
	.page-body-wrapper {
		margin-top: 0;
	}
	
	.page-body-info {
		padding-bottom: 50upx;
	}
	
	.time-big {
		font-size: 30upx;
		margin: 20upx;
	}
	
	.slider {
		width:530upx;
		padding-left: 10upx;
	}
	
	.play-time {
		font-size: 28upx;
		width:100%;
		padding: 20upx 0;
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
	}
	
	.page-body-buttons {
		display: flex;
		justify-content: space-around;
		margin-top: 100upx;
	}
	
	.page-body-button {
		width: 250upx;
		text-align: center;
	}
	.article-meta {
		padding: 10upx 40upx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		color: gray;
		.uni-media-list-logo {
			height: 50upx;
			width: 50upx;
			margin-right: 20upx;
		}
	}
	.article-text {
		font-size: 26upx;
		line-height: 50upx;
	}
	.price{
		color: $uni-color-primary;
	}

	.article-author,
	.article-time {
		margin: 0 20upx;
		font-size: 30upx;
	}	
	.article-content {
		padding: 0 30upx;
		overflow: hidden;
		font-size: 30upx;
		margin-bottom: 30upx;
	}
	
	page{
		background: $page-color-base;
		padding-bottom: 160upx;
	}
	/* 底部操作菜单 */
	.page-bottom{
		position:fixed;
		left: 30upx;
		bottom:30upx;
		z-index: 95;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 690upx;
		height: 100upx;
		background: rgba(255,255,255,.9);
		box-shadow: 0 0 20upx 0 rgba(0,0,0,.5);
		border-radius: 16upx;
		
		.p-b-btn{
			display:flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: $font-sm;
			color: $font-color-base;
			width: 96upx;
			height: 80upx;
			.yticon{
				font-size: 40upx;
				line-height: 48upx;
				color: $font-color-light;
			}
			&.active, &.active .yticon{
				color: $uni-color-primary;
			}
			
			&.active, &.active .uni-icon{
				color: $uni-color-primary;
			}
			
			.icon-fenxiang2{
				font-size: 42upx;
				transform: translateY(-2upx);
			}
			.icon-shoucang{
				font-size: 46upx;
			}
		}
		.action-btn-group{
			display: flex;
			height: 76upx;
			border-radius: 100px;
			overflow: hidden;
			box-shadow: 0 20upx 40upx -16upx #fa436a;
			box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
			background: linear-gradient(to right, #ffac30,#fa436a,#F56C6C);
			margin-left: 20upx;
			position:relative;
			&:after{
				content: '';
				position:absolute;
				top: 50%;
				right: 50%;
				transform: translateY(-50%);
				height: 28upx;
				width: 0;
				border-right: 1px solid rgba(255,255,255,.5);
			}
			.action-btn{
				display:flex;
				align-items: center;
				justify-content: center;
				width: 180upx;
				height: 100%;
				font-size: $font-base ;
				padding: 0;
				border-radius: 0;
				background: transparent;
			}
		}
	}
	.center-box {
		width: 500upx;
		height: 500upx;
	}
	.center-box .image {
		width: 100%;
		height: 100%;
	}
	.f-header{
		display:flex;
		align-items:center;
		height: 120upx;
		padding: 6upx 30upx 8upx;
		image{
			flex-shrink: 0;
			width: 80upx;
			height: 80upx;
			margin-right: 20upx;
		}
		.tit-box{
			flex: 1;
			display: flex;
			flex-direction: column;
		}
		.tit{
			font-weight: bold;
			font-size: $font-lg +10upx;
			color: #font-color-dark;
			line-height: 1.3;
		}
		.tit2{
			font-size: $font-sm;
			color: $font-color-light;
		}
		.icon-you{
			font-size: $font-lg +2upx;
			color: $font-color-light;
		}
		.showMore{
			color: $font-color-light;
		}
	}			
</style>
